<template>
	<view>
		<view class="mainpadding2">
			<view class="mainpadding_top ffffff radius">
				<view class="titletext hei_text bold">描述</view>
				<textarea class="textareaclass margin_top hei_text main_size bold" placeholder="请输入描述"  v-model="content"/>
			</view>
			<view class="mainpadding_top ffffff radius">
				<view class="titletext hei_text bold">上传图片</view>
				<view class="flexleft">
					<view class="itemimgBox flexcenter margin_top" v-for="(item,index) in images_text" :key="index">
						<view class="listleftimg">
							<image class="listleftimg radius" :src="item" mode=""></image>
							<view class="deleteimg" @click="deleimage(index,images,images_text)"></view>
						</view>
					</view>
					<view class="itemimgBox flexcenter margin_top" v-if="images_text.length<4" @click="uploadImg(4,images,images_text)">
						<image class="listleftimg radius" src="../static/image/system/upimg.png" mode=""></image>
					</view>
				</view>
			</view>
			<view class="mainpadding_top ffffff radius">
				<view class="titletext hei_text bold">上传视频</view>
				<view class="videobox radius flexcenter margin_top" v-if="video_text.length">
					<video :src="video_text[0]" class="videobox radius"></video>
					<view class="deleteimg" @click="deleimage(0,video,video_text)"></view>
				</view>
				<view class="videobox radius flexcenter margin_top"  v-if="!video_text.length"  @click="upvideo(video,video_text)">
					<image class="listleftimg radius" src="@/static/image/system/upvideo.png" mode=""></image>
				</view>
			</view>
			<view class="mainpadding_top ffffff radius flexbetween">
				<view class="titletext bold hei_text">是否需要视频讲解</view>
				<u-radio-group v-model="is_need_video" placement="row" circle>
					<u-radio activeColor="#3478F5" :customStyle="{marginRight: '30rpx'}" label="否" name="0"></u-radio>
					<u-radio activeColor="#3478F5" label="是" name="1"></u-radio>
				</u-radio-group>
			</view>
			<view class="mainpadding_top ffffff radius flexbetween">
				<view class="titletext bold hei_text">奖励价格</view>
				<input type="text" style="text-align: right;" placeholder="请输入奖励价格" class="main_size bold hei_text" v-model="price">
			</view>
			<view class="ffffff mainpadding_top radius">
				<view class="titletext bold hei_text">接单后回答时间</view>
				<view class="flexbetween margin_top">
					<view class="lable" :class="duration==10?'itemgl':''" @click="duration=10">10分钟</view>
					<view class="lable" :class="duration==20?'itemgl':''" @click="duration=20">20分钟</view>
					<view class="lable" :class="duration==30?'itemgl':''" @click="duration=30">30分钟</view>
					<view class="lable" :class="duration==40?'itemgl':''" @click="duration=40">40分钟</view>
				</view>
			</view>
			<view class="submitbtn margin_top5" @click="submit">发布</view>
		</view>
	</view>
</template>
<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				content:"",
				images:[],
				images_text:[],
				video:[],
				video_text:[],
				is_need_video:"0",
				duration:"10",
				price:"",
				listid:""
			}
		},
		onLoad(options) {
			this.listid = options.id
		},
		methods: {
			uploadImg(value, upImgs,upImgs_text) {//上传图片
				let _this = this;
				let num = Number(value) - upImgs.length;
				if (num <= 0) {
					return false;
				}
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(num, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs,upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(num, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs,upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			uploadImageService(url, upImg,upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					upImg.push(res.url)
					upImgs_text.push(image_item);
				}).catch(err => {});
			},
			upvideo(upvideo,upvideo_text) {//上传视频
				let uploadFile = ''
				let _this = this;
				// 1.选择要上传的视频
				uni.chooseVideo({
					maxDuration: 60, // 拍摄视频最长拍摄时间，单位秒。最长支持 60 秒。
					sourceType: ['album', 'camera'], // album 从相册选视频，camera 使用相机拍摄，默认为：['album', 'camera']
					success: function(res) {
						_this.uploadImageService(res.tempFilePath,upvideo,upvideo_text)
					}
				});
			},
			//刪除圖片
			deleimage(i, upimg,upimg_text) {
				upimg.splice(i, 1);
				upimg_text.splice(i, 1);
			},
			submit(){
				if(this.content==""){
					httpRequest.toast("请输入问题描述")
					return false
				}
				if(this.price==""){
					httpRequest.toast("请输入奖励价格")
					return false
				}
				httpRequest.request('/api/teacher/publish', 'POST', {
					content:this.content,
					images:this.images.join(),
					video:this.video.join(),
					is_need_video:this.is_need_video,
					price:this.price,
					duration:this.duration,
					teacher_id:this.listid
				}, false, false, true).then(res => {
					if(res.code==1){
						uni.redirectTo({
							url:"/pages_askquestion/wentizf?msg=" + encodeURIComponent(JSON.stringify(res.data))
						})
					}else{
						httpRequest.toast(res.msg)
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	/deep/.u-radio-group {
		flex: none !important;
	}
</style>
